package edu.dlust.trading_platform.ability;

import android.os.AsyncTask;
import android.util.Log;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import java.io.UnsupportedEncodingException;
import java.util.Properties;

public class SendMail extends AsyncTask<Void, Void, Void> {
    private static final String TAG = "SendMailTask";

    private String recipientEmail;
    private int verificationCode;

    public SendMail(String recipientEmail, int verificationCode) {
        this.recipientEmail = recipientEmail;
        this.verificationCode = verificationCode;
    }

    @Override
    protected Void doInBackground(Void... params) {
        try {
            Properties props = new Properties();
            props.put("mail.smtp.host", "smtphz.qiye.163.com");
            props.put("mail.smtp.port", "25");
            props.put("mail.smtp.auth", "true");
            props.put("mail.smtp.starttls.enable", "true");

            Session session = Session.getInstance(props, new Authenticator() {
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication("lsakurastudio@lsakura.top", "Wyh15099627178.");
                }
            });

            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress("lsakurastudio@lsakura.top", "课程设计2206020228"));
            message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipientEmail));
            message.setSubject("青禾二手账号验证码");
            message.setContent("<div style=\"position:relative;\"><includetail><div align=\"center\"><div class=\"open_email\" style=\"margin-left:8px;margin-top:8px;margin-bottom:8px;margin-right:8px;\"><div><br><span class=\"genEmailContent\"><div id=\"cTMail-Wrap\" style=\"word-break:break-all;box-sizing:border-box;text-align:center;min-width:320px;max-width:660px;border:1px solid #f6f6f6;background-color:#f7f8fa;margin:auto;padding:20px 0 30px;font-family:'helvetica neue',PingFangSC-Light,arial,'hiragino sans gb','microsoft yahei ui','microsoft yahei',simsun,sans-serif\"><div class=\"main-content\" style=\"\"><table style=\"width:100%;font-weight:300;margin-bottom:10px;border-collapse:collapse\"><tbody><tr style=\"font-weight:300\"><td style=\"width:3%;max-width:30px;\"></td><td style=\"max-width:600px;\"><p style=\"height:2px;background-color:#00a4ff;border:0;font-size:0;padding:0;width:100%;margin-top:20px;\"></p><div id=\"cTMail-inner\" style=\"background-color:#fff;padding:23px 0 20px;box-shadow:0px 1px 1px 0px rgba(122,55,55,0.2);text-align:left;\"><table style=\"width:100%;font-weight:300;margin-bottom:10px;border-collapse:collapse;text-align:left;\"><tbody><tr style=\"font-weight:300\"><td style=\"width:3.2%;max-width:30px;\"></td><td style=\"max-width:480px;text-align:left;\"><h1 id=\"cTMail-title\" style=\"font-weight:bold;font-size:20px;line-height:36px;margin:0 0 16px;\">用户邮箱验证</h1><p id=\"cTMail-userName\" style=\"font-size:14px;color:#333;line-height:24px;margin:0;\">尊敬的用户，您好！</p><p id=\"cTMail-userName\" style=\"line-height:24px;margin:0px;\"><span style=\"color:rgb(51,51,51);font-size:14px;\">请输入以下代码，完成邮箱验证。</span></p><p class=\"cTMail-content\" style=\"font-size:14px;color:rgb(51,51,51);line-height:24px;margin:6px 0px 0px;word-wrap:break-word;word-break:break-all;\"><a id=\"cTMail-btn\" href=\"目标链接\" title=\"点我验证'\" style=\"font-size:16px;line-height:45px;display:block;background-color:rgb(0,164,255);color:rgb(255,255,255);text-align:center;text-decoration:none;margin-top:20px;border-radius:3px;\">"+this.verificationCode+"</a></p></dl><dl style=\"font-size:14px;color:rgb(51,51,51);line-height:18px;\"><dd style=\"margin:0px 0px 6px;padding:0px;font-size:12px;line-height:22px;\"><p id=\"cTMail-sender\" style=\"font-size:14px;line-height:26px;word-wrap:break-word;word-break:break-all;margin-top:32px;\">此致<br><strong>青禾二手管理团队</strong></p></dd></dl></td><td style=\"width:3.2%;max-width:30px;\"></td></tr></tbody></table></div><div id=\"cTMail-copy\" style=\"text-align:center;font-size:12px;line-height:18px;color:#999;\"><table style=\"width:100%;font-weight:300;margin-bottom:10px;border-collapse:collapse\"><tbody><tr style=\"font-weight:300\"><td style=\"width:3.2%;max-width:30px;\"></td><td style=\"max-width:540px;\"><p style=\"text-align:center;margin:20px auto 14px auto;font-size:12px;color:#999;\">此为系统邮件，请勿回复</p></td><td style=\"width:3.2%;max-width:30px;\"></td></tr></tbody></table></div></td><td style=\"width:3%;max-width:30px;\"></td></tr></tbody></table></div></div></div></div></includetail></div>", "text/html; charset=utf-8");

            Transport.send(message);
        } catch (MessagingException e) {
            Log.e(TAG, "Error sending email", e);
        } catch (UnsupportedEncodingException e) {
            Log.e(TAG, "Unsupported encoding", e);
        }
        return null;
    }
}
